<template>
  <div class="app-container">
    <div class="search-box">
      <div class="form-box">
        <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" @submit.native.prevent>
          <el-form-item label="赛事名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入赛事名称" clearable :spellcheck="false"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="赛事类型" prop="type">
            <el-select v-model="queryParams.type" placeholder="请选择赛事类型" clearable>
              <el-option v-for="(value, key) in typeObj" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>
          <el-form-item label="赛事级别" prop="level">
            <el-select v-model="queryParams.level" placeholder="请选择赛事级别" clearable>
              <el-option v-for="(value, key) in levelObj" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>
          <el-form-item label="开放状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择开放状态" clearable>
              <el-option v-for="(value, key) in statusObj" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="btn-box">
        <div class="left-box">
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>
    <div class="handle-box">
      <el-button type="primary" icon="el-icon-plus" size="small" @click="showCompetitionsEditDialog()">新增</el-button>
    </div>
    <div class="table-box">
      <el-table v-loading="loading" :data="tableData" height="100%">
        <el-table-column v-for="item in tableColumn" :key="item.prop" :label="item.label" :prop="item.prop"
          :class-name="item.className" :width="item.width" :min-width="item.minWidth" :sortable="item.sortable"
          :align="item.align || 'center'" :show-overflow-tooltip="item.showOverflowTooltip" :fixed="item.fixed"
          :resizable="false">
          <template slot-scope="{row}">
            <!-- 封面 -->
            <el-image v-if="['imageUrl',].includes(item.prop)" class="image" :src="row[item.prop]"
              :preview-src-list="[row[item.prop]]" />
            <!-- 赛事类型 -->
            <span v-else-if="item.prop == 'type'">{{ typeObj[row[item.prop]] }}</span>
            <!-- 赛事级别 -->
            <span v-else-if="item.prop == 'level'">{{ levelObj[row[item.prop]] }}</span>
            <!-- 状态 -->
            <span v-else-if="item.prop == 'status'">{{ statusObj[row[item.prop]] }}</span>
            <!-- 报名方式 -->
            <span v-else-if="item.prop == 'mode'">{{ modeObj[row[item.prop]] }}</span>
            <!-- 操作 -->
            <span v-else-if="item.prop == 'handle'" class="handle">
              <el-link type="primary" @click="showCompetitionsEditDialog(row)">修改</el-link>
              <el-link type="primary" @click="showCompetitionsRecordDialog(row)">报名记录</el-link>
            </span>
            <!-- 默认 -->
            <span v-else>{{ row[item.prop] }}{{ item.unit }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getData" />
    <!-- 弹窗动态组件 -->
    <component :is="dialogName" :visible.sync="dialogVisible" :params="dialogParams" :width="dialogWidth"
      :height="dialogHeight" @success="handleDialogSuccess" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import * as api from '@/api/competitionsManger/index.js';
// 批量局部注册组件
const componentsObj = {};
const req = require.context('./dialog', true, /\.vue$/);
req.keys().forEach((element, index) => {
  let name = element.replace(/(.*\/)|(\.vue)/gi, '');
  const config = req(element);
  componentsObj[name] = config.default || config;
});

export default {
  name: 'CompetitionsManger',
  components: componentsObj,
  data() {
    return {
      loading: true, // 遮罩层
      total: 0, // 总条数
      tableColumn: [
        { label: '赛事名称', prop: 'name', minWidth: 150 },
        { label: '封面', prop: 'imageUrl', minWidth: 120 },
        { label: '赛事类型', prop: 'type', minWidth: 100 },
        { label: '赛事级别', prop: 'level', minWidth: 100 },
        { label: '状态', prop: 'status', minWidth: 75 },
        { label: '举办时间', prop: 'eventTime', minWidth: 120 },
        { label: '地址', prop: 'location', minWidth: 150 },
        { label: '描述', prop: 'description', minWidth: 400, showOverflowTooltip: true },
        { label: '报名开始时间', prop: 'startTime', minWidth: 120 },
        { label: '报名结束时间', prop: 'endTime', minWidth: 120 },
        { label: '报名方式', prop: 'mode', minWidth: 75 },
        { label: '最大报名数', prop: 'maxTeamSize', minWidth: 85 },
        { label: '最小报名数', prop: 'minTeamSize', minWidth: 85 },
        { label: '最大年龄', prop: 'ageMax', minWidth: 75 },
        { label: '最小年龄', prop: 'ageMin', minWidth: 75 },
        { label: '操作', prop: 'handle', width: 90, fixed: 'right' },
      ],
      typeObj: { 1: '体育竞技', 2: '电子竞技', 3: '文化才艺' },
      levelObj: { 1: '市级', 2: '区级' },
      statusObj: { 0: '未开始', 1: '进行中', 2: '已结束' },
      modeObj: { 1: '个人', 2: '团队' },
      tableData: [], // 表格数据
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: '',
        type: '',
      },
      dialogName: null, // 弹窗组件名
      dialogVisible: false, // 弹窗显示
      dialogParams: {}, // 弹窗参数
      dialogWidth: '', // 弹窗宽度
      dialogHeight: '', // 弹窗高度
    };
  },
  computed: {
    ...mapGetters(['name']),
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 获取活动列表
    getData() {
      this.loading = true;
      api
        .getCompetitionsList(this.queryParams)
        .then((response) => {
          this.tableData = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          console.log('err', err);
          this.loading = false;
        });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getData();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 修改/新增弹窗
    showCompetitionsEditDialog(row) {
      this.dialogName = 'CompetitionsEditDialog';
      this.dialogVisible = true;
      this.dialogParams = { id: row?.id };
      this.dialogWidth = '510px';
      this.dialogHeight = 'auto';
    },
    // 记录弹窗
    showCompetitionsRecordDialog(row) {
      this.dialogName = 'CompetitionsRecordDialog';
      this.dialogVisible = true;
      this.dialogParams = { competitionId: row?.id };
      this.dialogWidth = '50%';
      this.dialogHeight = '80%';
    },
    // 处理弹窗success事件
    handleDialogSuccess(name, params) {
      if (['CompetitionsEditDialog'].includes(name)) {
        this.getData();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import '@/assets/styles/mixin.scss';
@include table-page-style;
</style>
